package org.lazy.generate;

import java.util.ArrayList;
import java.util.List;

import org.lazy.generate.bean.GeneratorContext;
import org.lazy.generate.bean.GeneratorDao;
import org.lazy.generate.bean.GeneratorDataSource;
import org.lazy.generate.bean.GeneratorEntity;
import org.lazy.generate.bean.GeneratorMapper;
import org.lazy.generate.bean.GeneratorService;
import org.lazy.generate.bean.GeneratorTable;
import org.lazy.util.StringUtil;
import org.lazy.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class GeneratorConfigXml {
	private static Document document = null;
	private static GeneratorConfigXml instance = null;
	public final static String DEFAULT_PRIMARY_KEY = "id";
	
	private GeneratorConfigXml() {}
	/**
	 * 单例模式
	 * @param resource 资源配置文件路径
	 * @return
	 */
    static GeneratorConfigXml getInstance(String resource) {
    	init(resource);
    	if(instance==null){
    		instance = new GeneratorConfigXml();
    	}
		return instance;
	}
    /**
     * 
     * @description 初始化
     * @author Anan
     * @time  2013-9-23 下午3:26:23
     */
    private static void init(String resource){
    	if(document==null){
    		String filepath = null;
    		filepath = Class.class.getResource("/").getPath() +resource;
        	document = XmlUtil.getDocument(filepath);  
    	}
    }
    
    List<GeneratorContext> getAllContext(){
		List<GeneratorContext> list = new ArrayList<GeneratorContext>();
		List<GeneratorTable> tables = null;
		GeneratorContext context = null;
		GeneratorMapper mapper = null;
		GeneratorTable table = null;
		GeneratorDataSource dataSource = null;
		GeneratorEntity entity = null;
		GeneratorDao dao = null;
		GeneratorService service = null;
		Node root = XmlUtil.getRoot(document);
		List<Node> targets = XmlUtil.getChildByNodeName("context", root);
		if(targets!=null){
			for(Node n:targets){
				//context
				context = new GeneratorContext();
				//datasource
				Node dataSourceNode = XmlUtil.getChildByNodeName("dataSource", n).get(0);				
				dataSource = new GeneratorDataSource();		
				dataSource.setDbType(XmlUtil.getNodeAttr( "dbType",dataSourceNode).toUpperCase());
				dataSource.setDriverClassName(XmlUtil.getNodeAttr("driverClassName",dataSourceNode));
				dataSource.setUrl(XmlUtil.getNodeAttr( "url",dataSourceNode));
				dataSource.setUsername(XmlUtil.getNodeAttr( "username",dataSourceNode));
				dataSource.setPassword(XmlUtil.getNodeAttr( "password",dataSourceNode));
				context.setDataSource(dataSource);
				//mapper
				Node mapperNode = XmlUtil.getChildByNodeName("mapper", n).get(0);
				mapper = new GeneratorMapper();
				mapper.setIsNeed(XmlUtil.getNodeAttr( "isNeed",mapperNode));
				mapper.setTargetPath(XmlUtil.getNodeAttr( "targetPath",mapperNode));
				mapper.setTargetPackage(XmlUtil.getNodeAttr( "targetPackage",mapperNode));
				context.setMapper(mapper);
				//entity
				Node entityNode = XmlUtil.getChildByNodeName("entity", n).get(0);
				entity = new GeneratorEntity();
				entity.setIsNeed(XmlUtil.getNodeAttr( "isNeed",entityNode));
				entity.setTargetPath(XmlUtil.getNodeAttr( "targetPath",entityNode));
				entity.setTargetPackage(XmlUtil.getNodeAttr( "targetPackage",entityNode));
				context.setEntity(entity);
				//dao
				Node daoNode = XmlUtil.getChildByNodeName("dao", n).get(0);
				dao = new GeneratorDao();
				dao.setIsNeed(XmlUtil.getNodeAttr( "isNeed",daoNode));
				dao.setTargetPath(XmlUtil.getNodeAttr( "targetPath",daoNode));
				dao.setTargetPackage(XmlUtil.getNodeAttr( "targetPackage",daoNode));
				context.setDao(dao);
				//service
				Node serviceNode = XmlUtil.getChildByNodeName("service", n).get(0);
				service = new GeneratorService();
				service.setIsNeed(XmlUtil.getNodeAttr( "isNeed",serviceNode));
				service.setTargetPath(XmlUtil.getNodeAttr( "targetPath",serviceNode));
				service.setTargetPackage(XmlUtil.getNodeAttr( "targetPackage",serviceNode));
				context.setService(service);
				//table
				List<Node> tableNodes = XmlUtil.getChildByNodeName("table", n);
				tables = new ArrayList<GeneratorTable>();
				String primaryKey = null;
				for(Node tNode:tableNodes){
					table = new GeneratorTable();
					table.setTableName(XmlUtil.getNodeAttr("tableName", tNode));
					table.setEntityName(XmlUtil.getNodeAttr("entityName", tNode));
					primaryKey = XmlUtil.getNodeAttr("primaryKey", tNode);
					if(StringUtil.isBlank(primaryKey)){
						primaryKey = DEFAULT_PRIMARY_KEY;
					}
					table.setPrimaryKey(primaryKey);
					tables.add(table);
				}
				context.setTables(tables);
				list.add(context);
			}
		}
		return list;
	}
    
}
